package com.zz.extension.config;

import com.zz.extension.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfigs {
    // 消息确认
    @Bean("AckQueue")
    public Queue AckQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }
    @Bean("AckExchange")
    public DirectExchange AckExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }
    public Binding AckBinding() {
        return BindingBuilder.bind(AckQueue()).to(AckExchange()).with("ack");
    }

    // TTl
    @Bean("TTLQueue")
    public Queue TTLQueue() {
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }
    @Bean("TTLQueue2")
    public Queue TTLQueue2() {
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(10000).build();
    }
    @Bean("TTLExchange")
    public DirectExchange TTLExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }
    @Bean("TTLBinding")
    public Binding TTLBinding() {
        return BindingBuilder.bind(TTLQueue()).to(TTLExchange()).with("ttl");
    }
    @Bean("TTLBinding2")
    public Binding TTLBinding2() {
        return BindingBuilder.bind(TTLQueue2()).to(TTLExchange()).with("ttl");
    }

    // 死信队列
    @Bean("NormalQueue")
    public Queue NormalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DLX_EXCHANGE)
                .deadLetterRoutingKey("dlx")
                .ttl(10000)
                .build();
    }
    @Bean("NormalExchange")
    public DirectExchange NormalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }
    @Bean("NormalBinding")
    public Binding NormalBinding() {
        return BindingBuilder.bind(NormalQueue()).to(NormalExchange()).with("normal");
    }
    @Bean("DLXQueue")
    public Queue DLXQueue() {
        return QueueBuilder.durable(Constants.DLX_QUEUE).build();
    }
    @Bean("DLXExchange")
    public DirectExchange DLXExchange() {
        return ExchangeBuilder.directExchange(Constants.DLX_EXCHANGE).build();
    }
    @Bean("DLXBinding")
    public Binding DLXBinding() {
        return BindingBuilder.bind(DLXQueue()).to(DLXExchange()).with("dlx");
    }

    // 延迟队列
    @Bean("DelayQueue")
    public Queue DelayQueue() {
        return QueueBuilder.durable(Constants.DELAY_QUEUE).build();
    }
    @Bean("DelayExchange")
    public DirectExchange DelayExchange() {
        return ExchangeBuilder.directExchange(Constants.DELAY_EXCHANGE).delayed().build();
    }
    @Bean("DelayBinding")
    public Binding DelayBinding() {
        return BindingBuilder.bind(DelayQueue()).to(DelayExchange()).with("delay");
    }

    // 事务
    @Bean("TransQueue")
    public Queue TransQueue() {
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }
}
